package com.sunhao.application;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.sunhao.application.util.ViewUtil;

import java.util.Random;

public class MainActivity7 extends AppCompatActivity {

    String checkCode;
    String mPassword = "666666";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main7);

        RadioGroup category_login = findViewById(R.id.category_login);
        EditText phone_number = findViewById(R.id.phone_number);
        EditText password_number = findViewById(R.id.password_number);
        TextView password_text = findViewById(R.id.password_text);
        Button password_forget = findViewById(R.id.password_forget);
        CheckBox remeber_password = findViewById(R.id.remeber_password);
        Button submit = findViewById(R.id.submit);

        category_login.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.password_login:
                        password_number.setHint("请输入密码");
                        password_text.setText("登陆密码：");
                        password_forget.setText("忘记密码");
                        remeber_password.setVisibility(View.VISIBLE);
                        break;
                    case R.id.checkCode_login:
                        password_number.setHint("请输入验证码");
                        password_text.setText("验证码：");
                        password_forget.setText("获取验证码");
                        remeber_password.setVisibility(View.INVISIBLE);
                        break;
                }
            }
        });


        //给手机文本框设置监听器
        phone_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 11) {
                    ViewUtil.hideOneInputMethod(MainActivity7.this, phone_number);
                }
            }
        });

        //给密码文本框设置监听器
        password_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 6) {
                    ViewUtil.hideOneInputMethod(MainActivity7.this, password_number);
                }
            }
        });


        //给忘记密码设置监听事件
        password_forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (phone_number.getText().length() < 11) {
//                    Toast.makeText(MainActivity7.this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
//                    return;
//                }

                //跳转至找回密码页面
                if (password_forget.getText().toString().equals("忘记密码")) {
//                        携带手机号码跳转到找回密码页面
                    Intent intent = new Intent(MainActivity7.this, LoginForgetActivity8.class);
                    intent.putExtra("phone", phone_number.getText().toString());
                    startActivityForResult(intent,1);
                    return;
                }


                if (password_forget.getText().toString().equals("获取验证码")) {
                    checkCode = String.format("%6d", new Random().nextInt(999999));
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity7.this);
                    builder.setTitle("请记住验证码");
                    builder.setMessage(String.format("您的手机号是%s本次验证码是:%s,请输入验证码", phone_number.getText().toString(), checkCode));
                    builder.setPositiveButton("好的", null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                    return;
                }



            }
        });

        //提交表单校验
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int checkedRadioButtonId = category_login.getCheckedRadioButtonId();
                switch (checkedRadioButtonId) {
                    case R.id.password_login:
//                        如果是密码校验登陆
                        if (!mPassword.equals(password_number.getText().toString())) {
                            Toast.makeText(MainActivity7.this, "密码不正确", Toast.LENGTH_SHORT).show();
                            return;
                        } else {
                            loginSuccess();
                        }
                        break;

                    case R.id.checkCode_login:
                        //如果是验证码登陆
                        if (!checkCode.equals(password_number.getText().toString())) {
                            Toast.makeText(MainActivity7.this, "验证码不正确", Toast.LENGTH_SHORT).show();
                            return;
                        } else {
                            loginSuccess();
                        }
                        break;
                }
            }
        });


    }

    private void loginSuccess() {
        TextView phone_number = findViewById(R.id.phone_number);
        String desc = String.format("您的手机号码是%s,恭喜您通过登陆验证,点击确定退出应用", phone_number.getText().toString());
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity7.this);
        builder.setTitle("登陆成功");
        builder.setMessage(desc);
        builder.setPositiveButton("退出应用", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finish();

            }
        });
        builder.setNegativeButton("我再看看", null);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }


    //接收上一个页面返回的消息
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("我执行了");
        //获取返回数据
//        if (data != null && requestCode == -1 && resultCode == 2) {
//            Bundle bundle = data.getExtras();
//            String password = bundle.getString("password");
//            mPassword = password;
//        }
    }


//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        System.out.println("requestCode:" + requestCode + "=====resultCode:" + resultCode);
//        if (requestCode == 1) {
//            Toast.makeText(this, "YES", Toast.LENGTH_SHORT).show();
//        } else {
//            Toast.makeText(this, "NO", Toast.LENGTH_SHORT).show();
//        }
//    }



}